package com.devshephard.jucks.threadSafeCollections.blockingQueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

//同步队列
public class synchroizeQueue {

    public static void main(String[] args) {

        //同步阻塞队列，每次只能有一次存入和一次取出操作，当一轮存入和取出完毕后，再重复执行存入和取出操作。以此类推
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();


        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName() + " <= a");blockingQueue.put("a");
                System.out.println(Thread.currentThread().getName() + " <= b");blockingQueue.put("b");
                System.out.println(Thread.currentThread().getName() + " <= c");blockingQueue.put("c");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"T1").start();

        new Thread(()->{

            try {
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + " => " + blockingQueue.take());
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + " => " + blockingQueue.take());
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + " => " + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"T1").start();

    }

}
